<?php namespace Home\Controller; 

class CartController extends CommonController{

	public function index(){
		if(isset($_SESSION['uid'])){
			//获得订单id
			$dd = Db::table("360mall_order")->where("o_uid",$_SESSION['uid'])->pluck("o_id");
		$d = Db::table("360mall_olist")->where("ol_oid","$dd")->get();
		foreach ($d as $k => $v) {
		$d[$k]['goods'] = Db::table("360mall_goods")->where("g_id",$v['ol_gid'])->field("g_id","g_pic","g_shopprice")->get();	
		 }
		}
		
		

		if(!empty($_SESSION['cart'])){
		$data = Cart::getGoods(); 
		$id = Cart::getOrderId();
		$sum = Cart::getTotalPrice();
		foreach ($data as $k => $v) {
			$data[$k]['id'] = Db::table("360mall_goods")->where("g_id",$v['id'])->first();
		$data[$k]['sum']=$sum;	
		}


		View::with("data",$data);
        View::make();
	    }
	}
//				$data=array( 
//  'sid'=>'4d854bc6',// 唯一 sid，添加购物车时自动生成 
//  'num'=>88 
	 public function updateCart(){
	 	if(IS_AJAX){
 
	 		$num = $_POST['num'];
			$gsid = $_POST['gsid'];
//	 		p($_POST);
			$data = array(
			   "sid"=>$gsid,
			   "num"=>$num,
			); 
        Cart::update($data); 
		$datas = Cart::getGoods(); 
		$sum = Cart::getTotalPrice();
		foreach ($datas as $k => $v) {
			$datas[$k]['id'] = Db::table("360mall_goods")->where("g_id",$v['id'])->first();
		$datas[$k]['sum']=$sum;	
		}
       echo json_encode($datas);
	 	}
	 }
  //删除购物车中的某一条数据数据
  public function delCart(){
  	if(IS_AJAX){
           $num = 0;
		   $gsid = $_POST['gsid'];
		   $data = array(
			   "sid"=>$gsid,
			   "num"=>$num,
			); 
      Cart::update($data); 

$datas = $_SESSION['cart']['total'];
echo json_encode($datas);
  }
  }
  public function test(){
//	$datas = $_SESSION['cart']['total'];
//	p($datas);
	p($_SESSION);
  }
}